package com.cxs.studentManagerSystem.base;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Project: studentManagerSystem
 * @Author: cxs2014501@163.com
 * @Create: 2022/9/29 11:39
 * @Description:
 **/
public class BaseServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (null != method && !"".equals(method)) {
            try {
                Method handleMethod = this.getClass().getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class);
                if (null != handleMethod) {
                    handleMethod.setAccessible(true);
                    Object invokeResult = handleMethod.invoke(this, req, resp);
                    if (null != invokeResult) {
                        if (String.class.isInstance(invokeResult)) {
                            String result = (String) invokeResult;
                            if (result.contains(":")) {
                                String[] strings = result.split(":");
                                if (null != strings && strings.length == 2) {
                                    if ("forward".equals(strings[0])) {
                                        req.getRequestDispatcher(strings[1]).forward(req, resp);
                                    } else if("redirect".equals(strings[0])) {
                                        resp.sendRedirect(strings[1]);
                                    } else {
                                        writeClientJson(new BaseResult(400, "不期待的操作方式", null), resp);
                                    }
                                }
                            }
                        } else if(BaseResult.class.isInstance(invokeResult)) {
                            writeClientJson((BaseResult) invokeResult, resp);
                        }
                    } else {
                        System.out.println("该处理方法无返回值");
                    }
                } else {
                    BaseResult result = new BaseResult(404, "处理方法不存在", null);
                    writeClientJson(result, resp);
                }
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } else {
            BaseResult result = new BaseResult(400, "method为必传项", null);
            writeClientJson(result, resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }


    private void writeClientJson(BaseResult result, HttpServletResponse response){
        try {
            PrintWriter writer = response.getWriter();
            response.setContentType("application/json;charset=utf-8");
            response.setStatus(result.getCode());
            String jsonString = JSON.toJSONString(result,
                    SerializerFeature.PrettyFormat,
                    SerializerFeature.WriteMapNullValue,
                    SerializerFeature.WriteNullListAsEmpty,
                    SerializerFeature.DisableCircularReferenceDetect);
            writer.print(jsonString);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
